package cn.gemiman.daoyi.module.cms.controller.admin.articlecategory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.articlecategory.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articlecategory.CmsArticleCategoryDO;
import cn.gemiman.daoyi.module.cms.service.articlecategory.CmsArticleCategoryService;

@Tag(name = "管理后台 - 文章栏目")
@RestController
@RequestMapping("/cms/article-category")
@Validated
public class CmsArticleCategoryController {

    @Resource
    private CmsArticleCategoryService articleCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建文章栏目")
    @PreAuthorize("@ss.hasPermission('cms:article-category:create')")
    public CommonResult<Long> createArticleCategory(@Valid @RequestBody CmsArticleCategorySaveReqVO createReqVO) {
        return success(articleCategoryService.createArticleCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章栏目")
    @PreAuthorize("@ss.hasPermission('cms:article-category:update')")
    public CommonResult<Boolean> updateArticleCategory(@Valid @RequestBody CmsArticleCategorySaveReqVO updateReqVO) {
        articleCategoryService.updateArticleCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章栏目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:article-category:delete')")
    public CommonResult<Boolean> deleteArticleCategory(@RequestParam("id") Long id) {
        articleCategoryService.deleteArticleCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章栏目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:article-category:query')")
    public CommonResult<CmsArticleCategoryRespVO> getArticleCategory(@RequestParam("id") Long id) {
        CmsArticleCategoryDO articleCategory = articleCategoryService.getArticleCategory(id);
        return success(BeanUtils.toBean(articleCategory, CmsArticleCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章栏目分页")
    @PreAuthorize("@ss.hasPermission('cms:article-category:query')")
    public CommonResult<PageResult<CmsArticleCategoryRespVO>> getArticleCategoryPage(@Valid CmsArticleCategoryPageReqVO pageReqVO) {
        PageResult<CmsArticleCategoryDO> pageResult = articleCategoryService.getArticleCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CmsArticleCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章栏目 Excel")
    @PreAuthorize("@ss.hasPermission('cms:article-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleCategoryExcel(@Valid CmsArticleCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmsArticleCategoryDO> list = articleCategoryService.getArticleCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章栏目.xls", "数据", CmsArticleCategoryRespVO.class,
                        BeanUtils.toBean(list, CmsArticleCategoryRespVO.class));
    }

}
